#include "MenuComponentParser.h"
#include "..\utils\PlainTextReader.h"
#include "..\action\BasicMenuAction.h"

namespace ds {

bool MenuComponentParser::isSupported(const std::string& fileEnding) const {
	bool ret = false;
	if ( fileEnding == ".menu" ) {
		ret = true;
	}
	return ret;
}
/*
void MenuComponentParser::createResource(ResourceDeclaration* declaration) {
	PlainTextReader reader;
	if ( reader.readFile(declaration->getName().c_str()) ) {
		Category* root = reader.getRoot();		
		std::string name = root->getProperty("name");
		std::string material = root->getProperty("material");
		std::string layer = root->getProperty("layer");
		BasicMenuAction* mc = new BasicMenuAction(name.c_str(),material,layer);		
		bool active = root->getBool("active",false);		
		if ( root->hasProperty("mouse_offset")) {
			Vec2 mo = root->getVec2("mouse_offset");
			mc->setMouseOffset(mo.x,mo.y);
		}
		std::vector<Category*> children = root->getChildren();
		for ( size_t i = 0; i < children.size(); ++i ) {
			Category* sub = children[i];
			if ( sub->getName() == "text_node" ) {
				std::string nodeName = sub->getProperty("name");
				std::string font = sub->getProperty("font");
				mc->createTextNode(nodeName,font);
			}
			else if ( sub->getName() == "text" ) {
				std::string node = sub->getProperty("text_node");
				Vec2 pos = sub->getVec2("pos");
				std::string txt = sub->getProperty("text");
				int index = sub->read<int>("index",-1);
				mc->addText(node,pos,txt,index);
			}
			else if ( sub->getName() == "link" ) {
				std::string node = sub->getProperty("text_node");
				Vec2 pos = sub->getVec2("pos");
				Vec2 size = sub->getVec2("size");
				std::string txt = sub->getProperty("text");
				int id = sub->read<int>("id",-1);
				Color color(1.0f,1.0f,1.0f,1.0f);
				if ( sub->hasProperty("color")) {
					color = sub->getColor("color");
				}
				if ( id != -1 ) {
					if ( sub->hasProperty("next_action")) {
						mc->addLink(id,node,pos.x,pos.y,size.x,size.y,txt,color,sub->getProperty("next_action"));
					}
					else {
						mc->addLink(id,node,pos.x,pos.y,size.x,size.y,txt,color);
					}
				}
				int eventID = sub->read<int>("event_id",-1);
				if ( eventID != -1 ) {
					mc->attachEvent(id,eventID);
				}
			}
			else if ( sub->getName() == "image" ) {
				Vec2 pos = sub->getVec2("pos");
				Rect rect = sub->getRect("rect");
				mc->addSprite(pos,rect);
			}
			else if ( sub->getName() == "image_link" ) {
				int id = sub->read<int>("id",-1);
				Vec2 pos = sub->getVec2("pos");
				Rect rect = sub->getRect("rect");
				int eventID = sub->read<int>("event_id",-1);
				mc->addImageLink(id,pos.x,pos.y,rect,eventID);
			}
		}
		if ( active )  {
			mc->start();
		}
		else {
			mc->stop();
		}
		gEngine->getActionManager().addAction(mc);
	}
}
*/
void MenuComponentParser::createResource(ResourceDeclaration* declaration) {
	JSONReader reader;
	if ( reader.parse(declaration->getName().c_str()) ) {
		std::vector<Category*>& all = reader.getCategories();
		Category* root = all[0];
		if ( root->getName() == "Menu" ) {
			std::string name = root->getProperty("name");
			std::string material = root->getProperty("material");
			std::string layer = root->getProperty("layer");
			BasicMenuAction* mc = new BasicMenuAction(name.c_str(),material,layer);		
			bool active = root->getBool("active",false);		
			if ( root->hasProperty("mouse_offset")) {
				Vec2 mo = root->getVec2("mouse_offset");
				mc->setMouseOffset(mo.x,mo.y);
			}
			if ( root->hasProperty("mouse_pointer")) {
				Rect rect = root->getRect("mouse_pointer");
				mc->setMousePointer(rect);
			}
			std::vector<Category*> children = root->getChildren();
			for ( size_t i = 0; i < children.size(); ++i ) {
				Category* sub = children[i];
				if ( sub->getName() == "text_node" ) {
					std::string nodeName = sub->getProperty("name");
					std::string font = sub->getProperty("font");
					mc->createTextNode(nodeName,font);
				}
				else if ( sub->getName() == "text" ) {
					std::string node = sub->getProperty("text_node");
					Vec2 pos = sub->getVec2("pos");
					std::string txt = sub->getProperty("text");
					int index = sub->read<int>("index",-1);
					float scale = sub->read<float>("scale",1.0f);
					mc->addText(node,pos,txt,index,scale);
				}
				else if ( sub->getName() == "link" ) {
					std::string node = sub->getProperty("text_node");
					Vec2 pos = sub->getVec2("pos");
					Vec2 size = sub->getVec2("size");
					std::string txt = sub->getProperty("text");
					int id = sub->read<int>("id",-1);
					Color color(1.0f,1.0f,1.0f,1.0f);
					if ( sub->hasProperty("color")) {
						color = sub->getColor("color");
					}
					if ( id != -1 ) {
						if ( sub->hasProperty("next_action")) {
							mc->addLink(id,node,pos.x,pos.y,size.x,size.y,txt,color,sub->getProperty("next_action"));
						}
						else {
							mc->addLink(id,node,pos.x,pos.y,size.x,size.y,txt,color);
						}
					}
					int eventID = sub->read<int>("event_id",-1);
					if ( eventID != -1 ) {
						mc->attachEvent(id,eventID);
					}
				}
				else if ( sub->getName() == "image" ) {
					Vec2 pos = sub->getVec2("pos");
					Rect rect = sub->getRect("rect");
					mc->addSprite(pos,rect);
				}
				else if ( sub->getName() == "image_link" ) {
					int id = sub->read<int>("id",-1);
					Vec2 pos = sub->getVec2("pos");
					Rect rect = sub->getRect("rect");
					int eventID = sub->read<int>("event_id",-1);
					mc->addImageLink(id,pos.x,pos.y,rect,eventID);
				}
			}
			if ( active )  {
				mc->start();
			}
			else {
				mc->stop();
			}
			gEngine->getActionManager().addAction(mc);
		}
		else {
			LOGC(logERROR,"MenuComponentParser") << "Invalid menu action definition found - skipping file";
		}
	}
}


ResourceDeclaration* MenuComponentParser::loadTextFile(const ResourceFile& resourceFile) {
	LOGC(logINFO,"MenuComponentParser") << "Adding basic menu action declaration for file " << resourceFile.fqName;
	BasicMenuActionDecl* td = new BasicMenuActionDecl(resourceFile.fqName);
	return td;
}

}
